Chrome Extension でユーザ入力を eval する
extension の設定で入力するなら options → background → content or sandbox の流れになる
例えばこういう感じで(実際にはコードのパースエラーとかも考慮する必要がある)
まああんまり使わないかな... Permission の要求も過剰になる
code:background.ts
chrome.tabs.executeScript({
file: "content.js"
}, () => {
chrome.tabs.sendMessage(
tab.id!,
{ code: "() => [${document.title}](${location.href})" },
res => console.log(res)
);
});
code:content.ts
chrome.runtime.onMessage.addListener(
(
message: any,
sender: chrome.runtime.MessageSender,
sendResponse: (response?: any) => void
) => {
const fn: () => string = eval(message.code);
let result: any;
try {
result = fn();
} catch (e) {
sendResponse({ error: e.message, errorType: "ExecutionError" });
}
sendResponse({ result });
return true;
}
);